package v1ch04.ConstructorTest;

import java.util.*;

/**
 * 该程序演示了对象构造。
 *
 * @author Cay Horstmann
 * @version 1.01 2004-02-19
 */
public class ConstructorTest {
	public static void main(String[] args) {
		// 用三个Employee对象填充staff数组
		Employee[] staff = new Employee[3];
		
		staff[0] = new Employee("Harry", 40000);
		staff[1] = new Employee(60000);
		staff[2] = new Employee();
		
		// 打印有关所有Employee对象的信息
		for (Employee e : staff)
			System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
					+ e.getSalary());
	}
}

class Employee {
	//类静态变量
	private static int nextId;
	
	//静态初始化块
	static {
		Random generator = new Random();
		//将nextId设置为0到9999之间的随机数
		nextId = generator.nextInt(10000);
	}
	
	private int id;
	private String name = ""; // 实例字段初始化
	private double salary;
	
	// 对象初始化块
	{
		id = nextId;
		nextId++;
	}
	
	//三个重载的构造函数
	public Employee(String n, double s) {
		name = n;
		salary = s;
	}
	
	public Employee(double s) {
		//调用Employee（String，double）构造函数
		this("Employee #" + nextId, s);
	}
	
	//默认构造函数
	public Employee() {
		//名称初始化为“”-参见上文
		// 未明确设置薪水-初始化为0
		// 在初始化块中初始化的id
	}
	
	public String getName() {
		return name;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public int getId() {
		return id;
	}
}
